﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;

namespace UserManage
{
    public class UserMangeMainFormViewModel:Models.BindableBase
    {        
        public string Title { get; set; }
        public plcitem PlcitemA { set; get; } = new plcitem(99);
        public plcitem PlcitemB { set; get; } = new plcitem(24);
        public plcitem PlcitemC { set; get; } = new plcitem(33);
    }

    public class plcitem : Models.BindableBase
    {
        public plcitem(int site)
        {
            Val = site;
            observer.Regedit(this);
        }

        public int Val { set; get; }
    }

    public class observer
    {
        static bool start = true;
        public static ObservableCollection<plcitem> Plcitems { set; get; } = new ObservableCollection<plcitem>() { };
        internal static void Regedit(plcitem pi)
        {
            Plcitems.Add(pi);
            if(!start)
            {
                Task.Run(async() =>
                {
                    while(true)
                    {
                        Plcitems.ToList().ForEach(x => x.Val++);
                        await Task.Delay(2000);
                    }                    
                });
                start = true;
            }
        }
    }
}
